package com.wsoft.cms.manager;

import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wsoft.cms.constant.CmsConstant;
import com.wsoft.cms.entity.CmsCategoryEntity;
import com.wsoft.cms.mapper.CmsCategoryMapper;
import com.wsoft.cms.vo.category.CmsCategoryInfoVO;
import com.wsoft.constant.redis.RedisKeyConstant;
import com.wsoft.core.annotation.GetRedisCache;
import com.wsoft.core.service.RedisService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author z001
 * @Date 2024/11/27 10:42
 */
@Slf4j
@Service
public class CmsCategoryManager extends ServiceImpl<CmsCategoryMapper, CmsCategoryEntity> {

    @Resource
    private RedisService redisService;


    @Resource
    private CmsCategoryAuthManager categoryAuthManager;

    /**
     * 返回当前code 最大值 (已经+1)
     *
     * @param paId
     * @return
     */
    public String getMaxCode(Long paId) {
        QueryWrapper<CmsCategoryEntity> query = new QueryWrapper<>();
        query.lambda().eq(null != paId, CmsCategoryEntity::getPaId, paId)
                .orderByDesc(CmsCategoryEntity::getCode)
                .last("limit 1");
        CmsCategoryEntity one = getOne(query);
        if (null == one) {
            if (null == paId) {
                return CmsConstant.CATEGORY_CODE_FIRST_NUM;
            } else {
                CmsCategoryInfoVO paEntity = getById(paId);
                return paEntity.getCode() + CmsConstant.CATEGORY_CODE_FIRST_NUM;
            }
        } else {
            return Long.valueOf(one.getCode()) + 1 + "";
        }
    }

    @Override
    public boolean save(CmsCategoryEntity entity){
        boolean b = super.save(entity);
        if (b) {
            redisService.del(RedisKeyConstant.Cms.CMS_CATEGORY_TREE);
        }
        return b;
    }

    /**
     * 根据id搜索
     *
     * @param id
     * @return
     */
    @GetRedisCache(type = RedisKeyConstant.Cms.CMS_CATEGORY_INFO,
            expire_sec = RedisKeyConstant.Cms.EXPIRED,
            value = RedisKeyConstant.REDIS_SELF)
    public CmsCategoryInfoVO getById(Long id) {
        CmsCategoryEntity entity = super.getById(id);
        if(null == entity){
            return new CmsCategoryInfoVO();
        }
        CmsCategoryInfoVO convert = Convert.convert(CmsCategoryInfoVO.class, entity);
        List<Long> categoryId = categoryAuthManager.getByCategoryId(id);
        convert.setOrgIdList(categoryId);
        return convert;
    }


    @Override
    public boolean updateById(CmsCategoryEntity entity) {
        boolean b = super.updateById(entity);
        if (b) {
            redisService.del(RedisKeyConstant.Cms.CMS_CATEGORY_INFO + entity.getId());
            redisService.del(RedisKeyConstant.Cms.CMS_CATEGORY_TREE);
        }
        return b;
    }

    /**
     * list查询
     *
     * @param query
     * @return
     */
    public List<CmsCategoryEntity> listCategory(boolean isUser) {
        QueryWrapper<CmsCategoryEntity> query = new QueryWrapper<>();
        query.lambda().eq(isUser, CmsCategoryEntity::getEnableFlag, 1)
                .orderByAsc(CmsCategoryEntity::getSort);
        return list(query);
    }

    /**
     * 根据id删除
     *
     * @param id
     * @return
     */
    public boolean deleteById(Long id) {
        boolean b = super.removeById(id);
        if (b) {
            redisService.del(RedisKeyConstant.Cms.CMS_CATEGORY_INFO + id);
            redisService.del(RedisKeyConstant.Cms.CMS_CATEGORY_TREE);
        }
        return b;
    }
}
